<?php
/**
 * Pearl One functions and definitions
 *
 * Set up the theme and provides some helper functions, which are used in the theme as custom template
 * tags. Others are attached to action and filter hooks in WordPress to change core functionality.
 *
 * @package Pearlvn
 * @subpackage Pearl_One
 *
 */

/*
 * Pearl One setup.
 */
function pearlone_setup() {
	// Load theme textdomain
	/**
	 * load_theme_textdomain( $domain, $path )
	 *
	 * @param $domain
     * (string) (required) Unique identifier for retrieving translated strings.
	 *
	 * Default: None
	 *
	 * @param $path
	 * (unknown) (optional) The directory where the .mo file can be found (without the trailing slash).
	 *
	 * Default: false
	 *
	 * @return (bool) 
	 * This function return TRUE as textdomain well loaded, FALSE on failure. 
	 */
	load_theme_textdomain( 'pearlone', get_template_directory() . '/languages' );

	//editor style
	/**
	 * add_editor_style( $stylesheet )
	 * 
	 * @param $stylesheet
	 * (string/array) (optional) Path to a stylesheet file, relative to the current theme directory, or 
	 * an array thereof to link multiple stylesheet files. If a child theme is used, both the current 
	 * child and parent theme directories are considered. See the Description section above. As of 
	 * version 3.6, a path may be absolute (beginning with http or https).
	 *
	 *  Default: "editor-style.css" 
	 *
	 * @return (void) 
	 * This function does not return a value. 
	 */
	add_editor_style( 'css/editor-style.css' );

}
add_action( 'after_setup_theme', 'pearlone_setup' );
/**
 * add_action( $hook, $function_to_add, $priority, $accepted_args );
 * 
 * @param $hook
 * (string) (required) The name of the action to which $function_to_add is hooked.
 * (See https://codex.wordpress.org/Plugin_API/Action_Reference for a list of action hooks). Can also
 * be the name of an action inside a theme or plugin file, or the special tag "all", in which case
 * the function will be called for all hooks)
 *
 * Default: None 
 *
 * @param $function_to_add
 * (callback) (required) The name of the function you wish to be hooked. Note: Only string-formatted
 * syntaxes listed in the PHP documentation for the 'callback' type are valid.
 *
 * Default: None 
 *
 *
 * @param $priority
 * (int) (optional) Used to specify the order in which the functions associated with a particular action
 * are executed. Lower numbers correspond with earlier execution, and functions with the same priority
 * are executed in the order in which they were added to the action.
 *
 * Default: 10 
 *
 * 
 * @param $accepted_args
 * (int) (optional) The number of arguments the hooked function accepts. In WordPress 1.5.1+, hooked
 * functions can take extra arguments that are set when the matching do_action() or apply_filters() call
 * is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID
 * of the requested comment.
 *
 * Default: 1 
 *
 * 
 * @return (boolean) 
 * Always True. 
 */
//-------------------------------------------------------------------------------------------------------------------------------------
/*
 * Enqueue scripts and styles for the front end
 */
function pearlone_scripts_styles() {
	// Chèn file style.css vào header
	/**
	 * wp_enqueue_style( $handle, $src, $deps, $ver, $media )
	 *
	 * @param $handle
	 * (string) (required) Name used as a handle for the stylesheet. As a special case, if the 
	 * string contains a '?' character, the preceding part of the string refers to the registered
	 * handle, and the succeeding part is appended to the URL as a query string.
	 *
	 * Default: None 
	 *
	 * @param $src
	 * (string|boolean) (optional) URL to the stylesheet. Example: 'http://example.com/css/mystyle.css'.
	 * This parameter is only required when WordPress does not already know about this style. You should
	 * never hardcode URLs to local styles, use plugins_url (for Plugins) and get_template_directory_uri
	 * (for Themes) to get a proper URL. Remote assets can be specified with a protocol-agnostic URL,
	 * i.e. '//otherdomain.com/css/theirstyle.css'.
	 *
	 * Default: false 
	 *
	 * @param $deps
	 * (array) (optional) Array of handles of any stylesheet that this stylesheet depends on; stylesheets
	 * that must be loaded before this stylesheet. false if there are no dependencies.
	 *
	 * Default: array() 
	 *
	 * @param $ver
	 * (string|boolean) (optional) String specifying the stylesheet version number, if it has one. This
	 * parameter is used to ensure that the correct version is sent to the client regardless of caching,
	 * and so should be included if a version number is available and makes sense for the stylesheet.
	 *
	 * Default: false
	 *
	 * @param $media
	 * (string|boolean) (optional) String specifying the media for which this stylesheet has been defined.
	 * Examples: 'all', 'screen', 'handheld', 'print'. See this list for the full range of valid CSS-media-types.
	 *
	 * Default: 'all'
	 *
	 * @return (void) 
	 * This function does not return a value. 
	 */
	wp_enqueue_style( 'style', get_stylesheet_uri(), array(), '2014-05-04', 'all' );

	// Chèn file function.js vào footer sử dụng jquery do đó đồng thời chèn jquery vào header
	/**
	 * wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer )
	 *
	 * @param $handle
	 * (string) (required) Name used as a handle for the script. As a special case, if the string contains a '?' character,
	 * the preceding part of the string refers to the registered handle, and the succeeding part is appended to the URL as
	 * a query string. A version must be used with this special case.
     *
	 * Default: None
	 *
	 * @param $src
	 * (string) (optional) URL to the script, e.g. http://example.com/wp-content/themes/my-theme/my-theme-script.js. You
	 * should never hardcode URLs to local scripts. To get a proper URL to local scripts, use plugins_url() for plugins and
	 * get_template_directory_uri() for themes. Remote scripts can be specified with a protocol-agnostic URL, 
	 * e.g. //otherdomain.com/js/their-script.js. This parameter is only required when the script with the given $handle
	 * has not been already registered using wp_register_script(). See Default Scripts Included and Registered by WordPress.
	 *
	 * Default: false
	 *
	 * @param $deps
	 * (array) (optional) Array of the handles of all the registered scripts that this script depends on, that is the scripts
	 * that must be loaded before this script. Set false if there are no dependencies. This parameter is only required when
	 * the script with the given $handle has not been already registered using wp_register_script(). Default handles are all in lower case.
	 *
	 * Default: array() 
	 *
	 * @param $ver
	 * (string) (optional) String specifying the script version number, if it has one, which is concatenated to the end of
	 * the path as a query string. If no version is specified or set to false, then WordPress automatically adds a version
	 * number equal to the current version of WordPress you are running. If set to null no version is added. This parameter
	 * is used to ensure that the correct version is sent to the client regardless of caching, and so should be included
	 * if a version number is available and makes sense for the script.
	 *
	 * Default: false
	 *
	 * @param $in_footer
	 * (boolean) (optional) Normally, scripts are placed in <head> of the HTML document. If this parameter is true, the script
	 * is placed before the </body> end tag. This requires the theme to have the wp_footer() template tag in the appropriate place.
	 *
	 * Default: false
	 *
	 * @return (void) 
	 * This function does not return a value. 
	 *
	 */
	wp_enqueue_script( 'functions', get_template_directory_uri().'/js/functions.js', array('jquery'), '2014-05-04', true );
	
}
add_action( 'wp_enqueue_scripts', 'pearlone_scripts_styles' );

// html, css body class 'blog' to 'site'
function pearlone_body_class($classes) {
	if (is_home()) {
		$key = array_search('blog', $classes);
		unset($classes[$key]);
	}
	return $classes;
}
add_filter( 'body_class', 'pearlone_body_class' );
/**
 * add_filter( $tag, $function_to_add, $priority, $accepted_args )
 * 
 * @param $tag
 * (string) (required) The name of the existing Filter to Hook the $function_to_add argument to. You can find a list of Filter Hooks
 * in http://codex.wordpress.org/Plugin_API/Filter_Reference.
 *
 * Default: None
 *
 * @param $function_to_add
 * (callback) (required) The name of the function to be called when the custom Filter is applied.
 *
 * Default: None
 *
 * @param $priority
 * (integer) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower
 * numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were
 * added to the filter.
 *
 * Default: 10
 *
 * @param $accepted_args
 * (integer) (optional) The number of arguments the function(s) accept(s). In WordPress 1.5.1 and newer hooked functions can
 * take extra arguments that are set when the matching apply_filters() call is run.
 *
 * Default: 1
 *
 * @return The function returns true if the attempted function hook succeeds or false if not. There is no test that the
 * function exists nor whether the $function_to_add is even a string. It is up to you to take care. This is done for
 * optimization purposes, so everything is as quick as possible. 
 * 
 */

/**
 * Create a nicely formatted and more specific title element text for output
 * in head of document, based on current view.
 *
 */
function pearlone_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() ) {
		return $title;
	}

	// Add the site name.
	$title .= get_bloginfo( 'name' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) ) {
		$title = "$title $sep $site_description";
	}

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 ) {
		$title = "$title $sep " . sprintf( __( 'Trang %s', 'pearlone' ), max( $paged, $page ) );
	}

	return $title;
}
add_filter( 'wp_title', 'pearlone_wp_title', 10, 2 );

/* customs in theme --------------------------------------------------------------------------------------------------------*/
// thiết lập các cài đặt
include get_template_directory().'/inc/settings.php';
